package com.xu.sso.web.service.impl;

import com.xu.sso.core.enums.ErrorMessage;
import com.xu.sso.core.exception.SSOException;
import com.xu.sso.core.model.BaseResponse;
import com.xu.sso.core.utils.ExceptionUtil;
import com.xu.sso.core.utils.LoginUtil;
import com.xu.sso.core.utils.MailUtil;
import com.xu.sso.db.model.base.User;
import com.xu.sso.db.service.AppDBService;
import com.xu.sso.db.service.RoleDBService;
import com.xu.sso.db.service.UserDBService;
import com.xu.sso.web.model.request.UpdateUserRequest;
import com.xu.sso.web.service.UserService;
import com.xu.sso.web.service.convert.UserConvert;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDBService userDBService;
    @Autowired
    private AppDBService appDBService;
    @Autowired
    private RoleDBService roleDBService;
    @Autowired
    private MailUtil mailUtil;

    @Override
    public User findUser(String username) {
        return userDBService.findUserByUsername(username);
    }

    @Override
    public List<User> findAllUser() {
        return userDBService.findAllUsers();
    }

    @Override
    public List<User> findAllUserByAppId(String appId) {
        return userDBService.findAppUsers(appId);
    }

    @Override
    public List<User> findAllUserNotInApp(String appId) {
        return userDBService.findNotAppUsers(appId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public BaseResponse updatePwd(String username, String newPwd, String oldPwd) throws SSOException {
        User user = userDBService.findUserByUsername(username);
        oldPwd = LoginUtil.encodePassword(username, oldPwd);
        if (!oldPwd.equals(user.getPassword())) {
            throw ExceptionUtil.createException(ErrorMessage.PasswordError);
        }
        newPwd = LoginUtil.encodePassword(username, newPwd);
        int i = userDBService.updatePassword(username, newPwd);
        if (i != 1) {
            throw ExceptionUtil.createException(ErrorMessage.UpdatePasswordError);
        }
        return new BaseResponse();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public BaseResponse updateUser(UpdateUserRequest request) throws SSOException {
        User user = UserConvert.convert(request);
        int i = userDBService.updateUserWithoutPwd(user);
        if (i != 1) {
            throw ExceptionUtil.createException(ErrorMessage.UpdateUserError);
        }
        return new BaseResponse();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public BaseResponse addUser(User user) throws SSOException {
        User oldUser = userDBService.findUserByUsername(user.getUsername());
        if (null != oldUser) {
            throw ExceptionUtil.createException(ErrorMessage.UserExist);
        }
        String pwd = RandomStringUtils.randomAlphanumeric(12);
        user.setPassword(LoginUtil.encodePassword(user.getUsername(), pwd));
        int i = userDBService.createUser(user);
        if (i != 1) {
            throw ExceptionUtil.createException(ErrorMessage.AddUserError);
        }
        // 发送邮件
        mailUtil.send(user.getMail(), "新用户注册", user.getUsername(), pwd);
        return new BaseResponse();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public BaseResponse deleteUser(String username) throws SSOException {
        // 删除AppUser
        appDBService.deleteAllAppUserByUser(username);
        // 删除AppUserRole
        roleDBService.deleteAppUserRolesByUser(username);

        // 删除user
        int i = userDBService.deleteUser(username);
        if (i != 1) {
            throw ExceptionUtil.createException(ErrorMessage.DeleteUserError);
        }
        return new BaseResponse();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public BaseResponse resetUser(String username) throws SSOException {
        User user = userDBService.findUserByUsername(username);
        String pwd = RandomStringUtils.randomAlphanumeric(12);
        int i = userDBService.updatePassword(username, LoginUtil.encodePassword(username, pwd));
        if (i != 1) {
            throw ExceptionUtil.createException(ErrorMessage.UpdatePasswordError);
        }
        // 发送邮件
        mailUtil.send(user.getMail(), "重置密码", username, pwd);
        return new BaseResponse();
    }

}
